#!/usr/bin/perl

# $ARGV[0] is the splice site file.
# $ARGV[1] is the bowtie results file, or "-" to read from standard input
# $ARGV[2] is indicates whether it's Burge or Illumina results.
# $ARGV[3] is the question mark to strand file.

my %strand_hash = ();
my %count_hash = ();
my %count_five = ();
my %count_three = ();

open (STRAND_FILE, "$ARGV[3]") || die "Can't open strand file:$ARGV[2]\n";

# Build has used to replace question mark strand info with plus or minus.
while ($line = <STRAND_FILE>){
     $line =~ s/\>//;
     chomp($line);
     @strand_map = split (/\t/, $line);
     $strand_hash{$strand_map[0]} = $strand_map[1];
#    print "@strand_map\t$strand_hash{$strand_map[0]}\n";
 }

close (STRAND_FILE);

open (SPLICE_FILE, "$ARGV[0]") || die "Can't open Splice Site file:$ARGV[0]\n";

# Read splice site file and use it to initialize hashes for counting splice site hits,
# five prime site hits and three prime site hits.
while ($line = <SPLICE_FILE>){
    if ($line =~ /\>/){
        $line =~ s/\>//;
        chomp($line);
        @remove_index = split (/:/, $line);
	@splice_name = split (/\_/, $remove_index[1]); 
        $chrom_name = $splice_name[0];
        $strand = $splice_name[1]; 
        if ($strand =~ /\?/){
	    $strand = $strand_hash{$line};
#           print "$line\t$strand\n";
	}
        if ($ARGV[2] =~ /Illumina/){
               $smaller_exon_position = $splice_name[4];
               $larger_exon_position = $splice_name[5];
	   }
        elsif ($ARGV[2] =~ /Burge/){
               $smaller_exon_position = $splice_name[2];
               $larger_exon_position = $splice_name[3];
	   }
        $smaller_intron_position = $smaller_exon_position + 1;
        $larger_intron_position = $larger_exon_position - 1;
        $splice_key = "$chrom_name"."_"."$strand"."_"."$smaller_exon_position"."_"."$larger_exon_position";
#       print "$splice_key\t";
        $count_hash{$splice_key} = 0;
        if ($strand =~ /\+/){
          $five_prime_key = "$chrom_name"."_+_"."$smaller_exon_position";
#         print "5E:$five_prime_key\t";
          $count_five{$five_prime_key} = 0;
          $five_prime_key = "$chrom_name"."_+_"."$larger_intron_position";
#         print "5I:$five_prime_key\t";
          $count_five{$five_prime_key} = 0;
          $splice_key =  "$chrom_name"."_+_"."$smaller_exon_position"."_"."$smaller_intron_position";
#         print "5IR:$splice_key\t";
          $count_hash{$splice_key} = 0;
          $three_prime_key = "$chrom_name"."_+_"."$larger_exon_position";
 #        print "3E:$three_prime_key\t";
          $count_three{$three_prime_key} = 0;
          $three_prime_key = "$chrom_name"."_+_"."$smaller_intron_position";
 #        print "3I:$three_prime_key\t";
          $count_three{$three_prime_key} = 0;
          $splice_key = "$chrom_name"."_+_"."$larger_intron_position"."_"."$larger_exon_position";
 #        print "3IR:$splice_key";
          $count_hash{$splice_key} = 0;
        }
        elsif ($strand =~ /\-/){
          $five_prime_key = "$chrom_name"."_-_"."$larger_exon_position";
#         print "5E:$five_prime_key\t";
          $count_five{$five_prime_key} = 0;
          $five_prime_key = "$chrom_name"."_-_"."$smaller_intron_position";
#         print "5I:$five_prime_key\t";
          $count_five{$five_prime_key} = 0;
          $splice_key =  "$chrom_name"."_-_"."$larger_intron_position"."_"."$larger_exon_position";
#         print "5IR:$splice_key\t";
          $count_hash{$splice_key} = 0;
          $three_prime_key = "$chrom_name"."_-_"."$smaller_exon_position";
#         print "3E:$three_prime_key\t";
          $count_three{$three_prime_key} = 0;
          $three_prime_key = "$chrom_name"."_-_"."$larger_intron_position";
#         print "3I:$three_prime_key\t";
          $count_three{$three_prime_key} = 0;
          $splice_key = "$chrom_name"."_-_"."$smaller_exon_position"."_"."$smaller_intron_position";
#         print "3IR:$splice_key\t";
          $count_hash{$splice_key} = 0; 
        }   
#       print "\n";
    }
}

close SPLICE_FILE;

if ($ARGV[1] eq "-") {
    $BOWTIE_FILE = STDIN;
    print "Reading from stdin...";
} else {
    open ($BOWTIE_FILE, "$ARGV[1]") || die "Can't open Bowtie file:$ARGV[1]\n";
}

while (<$BOWTIE_FILE>){
    @bowtie_line = split(/\t/);
    # print "bowtie_line:", @bowtie_line, "\n";
    if ($bowtie_line[2] =~ /\:/){
        @remove_index = split (/:/, $bowtie_line[2]);
	@splice_name = split (/\_/, $remove_index[1]); 
        $chrom_name = $splice_name[0];
        $strand = $splice_name[1]; 
        if ($strand =~ /\?/){
#           print "@bowtie_line";
#	    print "$strand\t";
	    $strand = $strand_hash{$bowtie_line[2]};
#           print "$strand\n";
	}
        if ($ARGV[2] =~ /Illumina/){
               $smaller_exon_position = $splice_name[4];
               $larger_exon_position = $splice_name[5];
	   }
        elsif ($ARGV[2] =~ /Burge/){
               $smaller_exon_position = $splice_name[2];
               $larger_exon_position = $splice_name[3];
	   }
       $splice_key = "$chrom_name"."_"."$strand"."_"."$smaller_exon_position"."_"."$larger_exon_position";
       $count_hash{$splice_key}++;
       if ($strand =~ /\+/){
            $five_prime_key = "$chrom_name"."_+_"."$smaller_exon_position";
            $three_prime_key = "$chrom_name"."_+_"."$larger_exon_position";
	}
        elsif ($strand =~ /\-/){
            $five_prime_key = "$chrom_name"."_-_"."$larger_exon_position";
            $three_prime_key = "$chrom_name"."_-_"."$smaller_exon_position";
	  
	}
        $count_five{$five_prime_key}++;
        $count_three{$three_prime_key}++;
#       print "$bowtie_line[2]\n";
#        print "$splice_key\t$count_hash{$splice_key}\t$five_prime_key\t$count_five{$five_prime_key}\t$three_prime_key\t$count_three{$three_prime_key}\n";
    }
# Count intron retention junctions.
    elsif ($bowtie_line[2] =~ /chr/){
        $chrom_name = $bowtie_line[2]; 
        $first_position = $bowtie_line[3] + 1 + 3;
        $last_position = $bowtie_line[3] + 1 + 28;
        for ($i = $first_position; $i < $last_position; $i++){
	    $i_plus_one = $i + 1;
            $pos_splice_site = "$chrom_name"."_+_"."$i"."_"."$i_plus_one";
            $neg_splice_site = "$chrom_name"."_-_"."$i"."_"."$i_plus_one";
	    if (exists $count_hash{$pos_splice_site}){
                $difference = $i - $bowtie_line[3];
#               print "It Exists!\t$bowtie_line[2]\t$bowtie_line[3]\t$difference\t";
		$count_hash{$pos_splice_site}++;
                $five_prime_key = "$chrom_name"."_+_"."$i";
                $count_five{$five_prime_key}++;
                $three_prime_key = "$chrom_name"."_+_"."$i_plus_one";
                $count_three{$three_prime_key}++;
#               print "$pos_splice_site\t$count_hash{$pos_splice_site}\t$five_prime_key\t$count_five{$five_prime_key}\t$three_prime_key\t$count_three{$three_prime_key}\n";
	    }
            if  (exists $count_hash{$neg_splice_site}){
                $difference = $i - $bowtie_line[3];
#               print "It Exists!\t$bowtie_line[2]\t$bowtie_line[3]\t$difference\t";
		$count_hash{$neg_splice_site}++;
                $five_prime_key = "$chrom_name"."_-_"."$i_plus_one";
                $count_five{$five_prime_key}++;
                $three_prime_key = "$chrom_name"."_-_"."$i";
                $count_three{$three_prime_key}++;
 #              print "$neg_splice_site\t$count_hash{$neg_splice_site}\t$five_prime_key\t$count_five{$five_prime_key}\t$three_prime_key\t$count_three{$three_prime_key}\n";
	    }
	} 
    }
}

if ($BOWTIE_FILE != STDIN) {
    close $BOWTIE_FILE;
}

for $key ( keys %count_hash ) { 
        print "$key\t$count_hash{$key}\t";
        @splice_name = split (/\_/, $key);
        $chrom_name = $splice_name[0];
        $strand = $splice_name[1];
        $smaller_position = $splice_name[2];
        $larger_position = $splice_name[3];
        if ($strand =~ /\+/){
          $five_prime_key = "$chrom_name"."_"."$strand"."_"."$smaller_position";         
          $three_prime_key = "$chrom_name"."_"."$strand"."_"."$larger_position";
          print "$count_five{$five_prime_key}\t";   
          print "$count_three{$three_prime_key}\n"; 
             
        }
        elsif ($strand =~ /\-/){
          $five_prime_key = "$chrom_name"."_"."$strand"."_"."$larger_position";   
          $three_prime_key = "$chrom_name"."_"."$strand"."_"."$smaller_position";                 
          print "$count_three{$three_prime_key}\t";
          print "$count_five{$five_prime_key}\n";
        }
    }

#for $key ( keys %count_five ) {
#   print "$key\t$count_five{$key}\n";
#}

#for $key ( keys %count_three) {
#   print "$key\t$count_three{$key}\n";
#}
